package cn.kgm.template.server.controller;

import cn.kgm.core.http.request.BaseController;
import cn.kgm.core.http.response.R;
import cn.kgm.template.dto.dto.TemplateConfigSaveDTO;
import cn.kgm.template.dto.dto.TemplateConfigUpdateDTO;
import cn.kgm.template.dto.query.TemplateConfigPageQuery;
import cn.kgm.template.dto.vo.TemplateConfigVO;
import cn.kgm.template.server.entity.TemplateConfig;
import cn.kgm.template.server.service.TemplateConfigService;
import cn.kgm.template.server.struct.TemplateConfigMappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * # 技术改变自己 #
 * 活动模板配置表
 *
 * @author kapp-i
 * @date 2024-03-18 14:47:46
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/templateConfig")
@Api(value = "TemplateConfig", tags = "活动模板配置表")
public class TemplateConfigController extends BaseController {

    @Autowired
    private TemplateConfigService templateConfigService;

    /**
     * 分页列表查询活动模板配置表
     */
    @ApiOperation(value = "分页查询", notes = "分页查询活动模板配置表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "long", paramType = "query", defaultValue = "20"),
    })
    @GetMapping(value = "/page")
    public R<IPage<TemplateConfigVO>> page(TemplateConfigPageQuery data) {
        IPage<TemplateConfig> page = getPage();
        // 构建不为null的查询条件
        TemplateConfig templateConfig =TemplateConfigMappers.INSTANCE.mapToEntity(data);
        LambdaQueryWrapper<TemplateConfig> wrapper = Wrappers.lambdaQuery(templateConfig);
        // 分页查询
            templateConfigService.page(page, wrapper);
        return R.success(page.convert((TemplateConfigMappers.INSTANCE::mapToEntity)));
    }

    /**
     * 根据id查询活动模板配置表详情
     */
    @ApiOperation(value = "查看详情", notes = "根据id查询活动模板配置表详情")
    @GetMapping(value = "/{id}")
    public R<TemplateConfigVO> get(@PathVariable(value = "id") Long id) {
        TemplateConfig templateConfig =templateConfigService.getById(id);
        return R.success(TemplateConfigMappers.INSTANCE.mapToEntity(templateConfig));
    }

    /**
     * 新增活动模板配置表
     */
    @ApiOperation(value = "新增", notes = "新增活动模板配置表")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody @Validated TemplateConfigSaveDTO data) {
        TemplateConfig templateConfig =TemplateConfigMappers.INSTANCE.mapToEntity(data);
            templateConfigService.save(templateConfig);
        return R.success();
    }

    /**
     * 修改活动模板配置表
     */
    @ApiOperation(value = "修改", notes = "修改活动模板配置表")
    @PostMapping("/update")
    public R<Boolean> update(@RequestBody @Validated TemplateConfigUpdateDTO data) {
        TemplateConfig templateConfig =TemplateConfigMappers.INSTANCE.mapToEntity(data);
        templateConfigService.updateById(templateConfig);
        return R.success();
    }

    /**
     * 根据id更新活动模板配置表的删除状态
     */
    @ApiOperation(value = "删除", notes = "根据id更新活动模板配置表的删除状态")
    @GetMapping(value = "/delete/{ids}")
    public R<Boolean> delete(@PathVariable(value = "ids") Long[] ids) {
            templateConfigService.update(Wrappers.<TemplateConfig>lambdaUpdate()
                .set(TemplateConfig::getIsDel, true)
                .in(TemplateConfig::getId, ids));
        return R.success();
    }
}
